<?php
class Easynote_Model_Record extends model {

    public $user_id = null;

    private $sqls = array(
        'get_page' => 'select `id`,`title`,`has_star`,`has_notice`,`book_id`,`geo_address`,`created`,`updated` from `mini_easynote_record` where `user_id` = [@user_id] order by `updated` desc limit [@page_index],[@page_size];',
        'get_page_default' => 'select `id`,`title`,`has_star`,`has_notice`,`book_id`,`geo_address`,`created`,`updated` from `mini_easynote_record` where `user_id` = [@user_id] and `book_id` = \'0\' order by `updated` desc limit [@page_index],[@page_size];',
        'get_page_star' => 'select `id`,`title`,`has_star`,`has_notice`,`book_id`,`geo_address`,`created`,`updated` from `mini_easynote_record` where `user_id` = [@user_id] and `has_star` = \'1\' order by `updated` desc limit [@page_index],[@page_size];',
        'get_page_by_book_id' => 'select `id`,`title`,`has_star`,`has_notice`,`book_id`,`geo_address`,`created`,`updated` from `mini_easynote_record` where `user_id` = [@user_id] and `book_id` = [@book_id] order by `updated` desc limit [@page_index],[@page_size];',
        'get_one'  => 'select `id`,`title`,`content`,`has_star`,`has_notice`,`book_id`,`geo_address`,`created`,`updated` from `mini_easynote_record` where `user_id` = [@user_id] and `id` = [@id];',
        'create'  => 'insert into `mini_easynote_record`(`id`,`title`,`content`,`user_id`,`book_id`,`geo_address`,`created`,`updated`) values([@id],[@title],[@content],[@user_id],[@book_id],[@geo_address],[@created],[@updated])',
        'update'  => 'update `mini_easynote_record` set {{@set}} where `id` = [@id] and `user_id` = [@user_id]',
        'delete'  => 'delete from `mini_easynote_record` where `record_id` = [@record_id] and `user_id` = [@user_id]',
        'get_all_count' => 'select count(1) from `mini_easynote_record` where `user_id` = [@user_id]',
        'get_default_count' => 'select count(1) from `mini_easynote_record` where `user_id` = [@user_id] and `book_id` = \'0\'',
        'get_count_by_book_id' => 'select count(1) from `mini_easynote_record` where `user_id` = [@user_id] and `book_id` = [@book_id]',
        'get_star_count' => 'select count(1) from `mini_easynote_record` where `user_id` = [@user_id] and `has_star` <> 0',
        'update_book_id' => 'update `mini_easynote_record` set `book_id` = [@book_id] where `id` = [@id] and `user_id` = [@user_id]',
    );
    
    public function __construct(){
        parent::__construct();
    }
    
    //list,支持分页
    public function get_page($page = 1, $page_size = 10){
        $page_index = ($page>=1)?($page-1)*10:0;
        return mysql::fetch('record', $this->sqls['get_page'], array('user_id'=>$this->user_id, 'page_index'=>$page_index, 'page_size'=>$page_size));
    }

    //
    public function get_page_default($page = 1, $page_size = 10){
        $page_index = ($page>=1)?($page-1)*10:0;
        return mysql::fetch('record', $this->sqls['get_page_default'], array('user_id'=>$this->user_id, 'page_index'=>$page_index, 'page_size'=>$page_size));
    }

    public function get_page_by_book_id($book_id, $page = 1, $page_size = 10){
        $page_index = ($page>=1)?($page-1)*10:0;
        return mysql::fetch('record', $this->sqls['get_page_by_book_id'], array('book_id'=>$book_id, 'user_id'=>$this->user_id, 'page_index'=>$page_index, 'page_size'=>$page_size));
    }

    public function get_page_star($page = 1, $page_size = 10){
        $page_index = ($page>=1)?($page-1)*10:0;
        return mysql::fetch('record', $this->sqls['get_page_star'], array('user_id'=>$this->user_id, 'page_index'=>$page_index, 'page_size'=>$page_size));
    }

    public function get_one($record_id){
        $record = mysql::fetch('record', $this->sqls['get_one'], array('user_id'=>$this->user_id, 'id'=>$record_id));
        if(!empty($record))
            return array_shift($record);
        else
            return false;
    }
    
    //获取所有记录数
    public function get_all_count(){
        $c = mysql::fetch('record', $this->sqls['get_all_count'], array('user_id'=>$this->user_id));
        return $c[0]['count(1)'];
    }
    
    //获取默认记录数
    public function get_default_count(){
        $c = mysql::fetch('record', $this->sqls['get_default_count'], array('user_id'=>$this->user_id));
        return $c[0]['count(1)'];
    }

    public function get_count_by_book_id($book_id){
        $c = mysql::fetch('record', $this->sqls['get_count_by_book_id'], array('book_id'=>$book_id, 'user_id'=>$this->user_id));
        return $c[0]['count(1)'];
    }

    //获取星标记记录数
    public function get_star_count(){
        $c = mysql::fetch('record', $this->sqls['get_star_count'], array('user_id'=>$this->user_id));
        return $c[0]['count(1)'];
    }
    
    //添加
    public function add($data){
        if(empty($data['title']) && empty($data['content'])) return false;
        $data['created'] = $data['updated'] = time();
        $data['id'] = md5($data['title'].$this->user_id.$data['created']);
        $data['user_id'] = $this->user_id;
        mysql::execute('record', $this->sqls['create'], $data);
        return $data['id'];
    }
    
    //修改
    public function edit($id, $data){
        $set = array();
        $data['updated'] = time();
        foreach($data as $key=>$value){
            $set[] = '`'.$key.'` = [@'.$key.']';
        }
        $sql = str_replace('{{@set}}',implode(',',$set),$this->sqls['update']);
        mysql::execute('record', $sql, $data+array('id'=>$id, 'user_id'=>$this->user_id));
        return true;
    }

    //更新记事本ID
    public function update_book_id($id, $book_id){
        mysql::execute('record', $this->sqls['update_book_id'], array('id'=>$id, 'book_id'=>$book_id, 'user_id'=>$this->user_id));
        return true;
    }

    //删除
    public function delete($id){
        mysql::execute('record', $this->sqls['delete'], array('id'=>$id, 'user_id'=>$this->user_id));
        return true;
    }
}